package com.ctriphotel.website.config;

import com.ctriphotel.website.interceptor.ResourceInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.file.Paths;

@Configuration
@EnableWebMvc
public class StaticResConfigurer extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String prefixPath = resolvePathPrefix();
        registry.addResourceHandler( "/assets/**")
                .addResourceLocations("file:" + prefixPath + "assets/");
    }


    private String resolvePathPrefix() {
        String rootPath = Paths.get(".").toUri().normalize().getPath();
        return rootPath;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(new ResourceInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}
